﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize]
    public class SellBackController : Controller
    {
       SellBack_BLL _SellBack_BLL = new SellBack_BLL();
       Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
       Customer_BLL _Customer_BLL = new Customer_BLL();
       VerifyState_BLL _VerifyState_BLL = new VerifyState_BLL();
       ContactOrder_BLL _ContactOrder_BLL = new ContactOrder_BLL();
          public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _SellBack_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
              // model = model.Where(a => a.UserName.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }


        public virtual ActionResult Details(Guid Id)
        {
            ViewBag.Sys_Users = _Sys_Users_BLL.Get();
            ViewBag.Customer = _Customer_BLL.Get();
            ViewBag.VerifyState = _VerifyState_BLL.Get();
            ViewBag.ContactOrder = _ContactOrder_BLL.Get();
            var item = _SellBack_BLL.Get(Id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? Id,string strCompanyId )
        {
            if (_ContactOrder_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加销售管理中的合同订单信息')</script>");
                Response.End();
            }
            
            var customer = _Customer_BLL.Get();
            var customercontact =_ContactOrder_BLL.Get ().Where(a=>a.State.Equals("已批准"));
            var customerId = customer.FirstOrDefault().Id;
            
            if (!string.IsNullOrEmpty(strCompanyId))
            {
                customerId = new Guid(strCompanyId);
                SelectList listItem = new SelectList(customer.AsEnumerable(), "Id", "CustomerName", customerId);
                ViewData.Add("Customer", listItem);
            }
            else
            {
                SelectList listItem = new SelectList(customer.AsEnumerable(), "Id", "CustomerName");
                ViewData.Add("Customer", listItem);
            }
            customercontact = customercontact.Where(a => a.CustomerId == customerId);

            SelectList listItem2 = new SelectList(customercontact.AsEnumerable(), "Id", "Number");
            ViewData.Add("CustomerContact", listItem2);

         
            ViewBag.Sys_Users = _Sys_Users_BLL.Get();
            
            ViewBag.VerifyState = _VerifyState_BLL.Get();
   
            var item = _SellBack_BLL.Get(Id);
            if (Id == null)
            {
                ViewData["Id"] = null;
                item.BackDate = DateTime.Now;
               
            }
            else
            {
                ViewData["Id"] = Id;

            }
            return View(item);
        }

        [HttpPost]
        [ValidateInput(false)]
         public virtual ActionResult Edit(Guid? Id, SellBack collection)
        {
            if (Request["CustomerId"] == null)
            {
                ModelState.AddModelError("CustomerId", "客户名称不能为空");
            }
            if (Request["ContactOrderId"] == null)
            {
                ModelState.AddModelError("ContactOrderId", "合同单编号不能为空");
            }
            if (!ModelState.IsValid)
            {
                Edit(Id,"");
                return View(collection);
            }
            _SellBack_BLL.Save(Id, ref collection);
            return RedirectToAction("Index",MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        { 
            _SellBack_BLL.Delete(Id);
            return RedirectToAction("Index",MyrouteValues.Get());
        }
    }
}
